package xkdaima;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class XTcontrol
{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        /*
        System.out.println("请设置维护开始时间与结束时间(格式：yyyy-MM-dd-HH-mm-ss)：");
        String l= input.next();
        String p= input.next();
        Thread1 thread1 = new Thread1(l,p);
        thread1.start();
         */
        System.out.println("请设置选课开始时间与结束时间(格式：yyyy-MM-dd-HH-mm-ss)：");
        String a= input.next();
        String b= input.next();
        Thread2 thread2 = new Thread2(a,b);
        thread2.start();
    }
}

class Thread1 extends Thread{
    boolean bf=true;
    boolean bool=false;
    String l,p;
    public Thread1(String l,String p){
        super();
        this.l=l;
        this.p=p;
    }
    public void run() {
        while (true) {
            try {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                Calendar calendar = Calendar.getInstance();
                Date date = calendar.getTime();
                String dateStringParse = sdf.format(date);
                CourseMaintain(dateStringParse,l,p);
                if(bool) break;
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void CourseMaintain(String x, String l, String p){
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:sorce/xuanke.sqlite");
            SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            Date date1=new Date();
            date1=a.parse(l);
            Date date2=new Date();
            date2=a.parse(p);
            Date date3=new Date();
            date3=a.parse(x);
            try {
                if((date3.compareTo(date1))==0&&(date3.compareTo(date1))>0&&bf)
                {
                    PreparedStatement pst=connection.prepareStatement("update CTL set IfMCourse=1");
                    pst.executeUpdate();
                    System.out.println("课程维护开始!");
                    pst.close();
                    bf=false;
                }
                else if((date3.compareTo(date2))==0&&(date3.compareTo(date2))>0){
                    PreparedStatement pst=connection.prepareStatement("update CTL set IfMCourse=0");
                    pst.executeUpdate();
                    System.out.println("课程维护结束!");
                    pst.close();
                    bool=true;
                }
                connection .close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
class Thread2 extends Thread{
    boolean bf=true;
    boolean bool=false;
    String a,b;
    public Thread2(String a,String b){
        super();
        this.a=a;
        this.b=b;
    }
    public void run() {
        while (true) {
            try {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                Calendar calendar = Calendar.getInstance();
                Date date = calendar.getTime();
                String dateStringParse = sdf.format(date);
                CourseSelect(dateStringParse,a,b);
                if(bool) break;
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void CourseSelect(String x,String l,String p){
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:sorce/xuanke.sqlite");
            SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            Date date1=new Date();
            date1=a.parse(l);
            Date date2=new Date();
            date2=a.parse(p);
            Date date3=new Date();
            date3=a.parse(x);
            try {
                if((date3.compareTo(date1))==0&&(date3.compareTo(date1))>0&&bf)
                {
                    PreparedStatement pst=connection.prepareStatement("update CTL set IfSCourse=1");
                    pst.executeUpdate();
                    System.out.println("选课开始!");
                    pst.close();
                    bf=false;
                }
                else if((date3.compareTo(date2))==0&&(date3.compareTo(date2))>0){
                    PreparedStatement pst=connection.prepareStatement("update CTL set IfSCourse=0");
                    pst.executeUpdate();
                    System.out.println("选课结束!");
                    pst.close();
                    bool=true;
                }
                connection .close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

